﻿using AutoMapper;
using Enferno.Public.Web.Models;
using Enferno.StormApiClient.Customers;

namespace Enferno.Public.Web.Mappers.CheckoutProfiles
{
    public class CustomerToCustomerModelProfile :
        CustomerToCustomerBaseModelProfile
            <CustomerModel, CompanyModel, CheckoutBuyerModel, CompanyInformationModel, PersonInformationModel,
                AddressModel>
    {
        
    }

    public class CustomerToCustomerBaseModelProfile<TCustomerModel, TCompanyModel, TCheckoutBuyerModel,
        TCompanyInformationModel, TPersonInformationModel, TAddressModel> : Profile
        where TCustomerModel : CustomerBaseModel<TCompanyModel, TCompanyInformationModel, TPersonInformationModel, TAddressModel>
        where TCompanyModel : CompanyBaseModel<TCompanyInformationModel, TAddressModel>
        where TCheckoutBuyerModel : CheckoutBuyerModel
        where TCompanyInformationModel : CompanyInformationModel
        where TPersonInformationModel : PersonInformationModel
        where TAddressModel : AddressModel
    {
        protected override void Configure()
        {
            Mapper.CreateMap<Customer, PersonInformationModel>()
                .Include<Customer, TCheckoutBuyerModel>();

            Mapper.CreateMap<Customer, TCheckoutBuyerModel>()
                .ForMember(to => to.CustomerId, opts => opts.MapFrom(from => from.Id));

            Mapper.CreateMap<TCheckoutBuyerModel, Customer>()
                .ForMember(to => to.Id, opts => opts.MapFrom(from => from.CustomerId))
                .ForMember(to => to.Key, opts => opts.Ignore())
                .ForMember(to => to.Code, opts => opts.Ignore())
                .ForMember(to => to.SSN, opts => opts.Ignore())
                .ForMember(to => to.ReferId, opts => opts.Ignore())
                .ForMember(to => to.ReferUrl, opts => opts.Ignore())
                .ForMember(to => to.Account, opts => opts.Ignore())
                .ForMember(to => to.Companies, opts => opts.Ignore())
                .ForMember(to => to.DeliveryAddresses, opts => opts.Ignore())
                .ForMember(to => to.InvoiceAddress, opts => opts.Ignore())
                .ForMember(to => to.Flags, opts => opts.Ignore())
                .ForMember(to => to.UseInvoiceAddressAsDeliveryAddress, opts => opts.Ignore())
                .ForMember(to => to.Info, opts => opts.Ignore())
                .ForMember(to => to.ExtensionData, opts => opts.Ignore());

            Mapper.CreateMap<Customer, TCustomerModel>()
                .ForMember(to => to.Person,
                    opts =>
                        opts.MapFrom(from => Mapper.Map<Customer, TPersonInformationModel>(from)));
        }

        public override string ProfileName
        {
            get { return GetType().Name; }
        }
    }
}
